﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Configuration;


namespace ElectronicJournal
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Page.Title = h1Header.InnerText = "Welcome To " + ConfigurationManager.AppSettings["name"].ToString();
            filBanner();
        }

        protected void HeadLoginView_ViewChanged(object sender, EventArgs e)
        {

        }

        private void filBanner()
        {

            byte[] topBanner = getBannerFromDb("верх");
            byteArrayToImage(topBanner);
            //setBannerFromByte(topBanner, imgTopBanner);
        }

        private void setBannerFromByte(byte[] imgS, Image imgCtr)
        {
            MemoryStream stream = new MemoryStream(imgS);
            System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
            string strPath = Server.MapPath("topBanner.bmp");
            img.Save(strPath);
            imgCtr.ImageUrl = strPath;
        }
        byte[] getBannerFromDb(string position)
        {
            byte[] res = new byte[8000];
            try
            {

                int fileID;
                SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
                cnn.Open();
                SqlDataAdapter da = new SqlDataAdapter("select banner.conten from  banner where position=@pos", cnn);
                da.SelectCommand.Parameters.AddWithValue("@pos", position);
                //SqlCommand cmd = new SqlCommand();

                //cmd.Connection = cnn;

                //cmd.CommandText = "select banner.content from  banner where position=@pos";

                //cmd.CommandType = CommandType.Text;

                //cmd.Parameters.Add("@pos", SqlDbType.NChar);
                //cmd.Parameters["@pos"].Value = position;


                //SqlDataAdapter da = new SqlDataAdapter();

                //da.SelectCommand = cmd;

                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    res = (byte[])dt.Rows[0][0];
                }

            }
            catch
            {
            }
            return res;

        }

        private static string GetTempFolderName()
        {
            string strTempFolderName = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache) + @"\";

            if (Directory.Exists(strTempFolderName))
            {
                return strTempFolderName;
            }
            else
            {
                Directory.CreateDirectory(strTempFolderName);
                return strTempFolderName;
            }
        }

        private void byteArrayToImage(byte[] byteArrayIn)
        {
            System.Drawing.Image newImage;
            try
            {

                string strFileName = GetTempFolderName() + "yourfilename.gif";
                if (byteArrayIn != null)
                {
                    using (MemoryStream stream = new MemoryStream(byteArrayIn))
                    {
                        newImage = System.Drawing.Image.FromStream(stream);

                        newImage.Save(strFileName);

                        //imgTopBanner.Attributes.Add("src", strFileName);
                        //String strNew = strFileName.Replace("\\", "/");
                        //imgTopBanner.ImageUrl = "C:/Users/thuy trang/Desktop/948167960.gif";

                        string aa = "yourfilename.gif";
                        //imgTopBanner.ImageUrl = "Handler.ashx?id=" + aa;
                    }


                }
                else
                {
                    Response.Write("No image data found!");
                }
            }
            catch
            {
            }
        }
    }
}
